//package com.qijian.plug.capacity.cost;
//
//import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
//import com.qijian.common.utils.StringUtils;
//import com.qijian.finance.domain.Cost;
//import com.qijian.finance.domain.enums.CostTypeEnum;
//import com.qijian.inventory.domain.OutboundConfirm;
//import com.qijian.inventory.domain.OutboundConfirmDetail;
//import com.qijian.inventory.service.IOutboundConfirmDetailService;
//import com.qijian.maindata.domain.Item;
//import com.qijian.maindata.service.IItemService;
//import lombok.RequiredArgsConstructor;
//import org.apache.commons.collections4.CollectionUtils;
//import org.springframework.stereotype.Component;
//import org.springframework.transaction.annotation.Transactional;
//
//import java.math.BigDecimal;
//import java.util.*;
//import java.util.stream.Collectors;
//
///**
// * 材料成本-领料出库确认单
// */
//@Component
//@RequiredArgsConstructor
//public class OutboundConfirmCost extends CostPlugWrapper<OutboundConfirm.Cost> {
//
//    private final IOutboundConfirmDetailService outboundConfirmDetailService;
//    private final IItemService itemService;
//
//    @Override
//    @SuppressWarnings("unchecked")
//    protected void operation(List<Object> args) {
//        Object param = args.get(0);
//        if (param == null) return;
//        if (param instanceof Long) {
//            OutboundConfirmDetail query = new OutboundConfirmDetail();
//            query.setMainId((Long) param);
//            List<OutboundConfirmDetail> confirmDetails = outboundConfirmDetailService.list(new QueryWrapper<>(query));
//            process(confirmDetails);
//        } else if (param instanceof List) {
//            process((List<OutboundConfirmDetail>) param);
//        }
//
//    }
//
//    private void process(List<OutboundConfirmDetail> temp) {
//        //过滤没有项目机台号的&数量小于0&价格小于0
//        List<OutboundConfirmDetail> confirmDetails = temp.stream()
//                .filter(s -> StringUtils.isNotBlank(s.getProjectNo()) &&
//                        s.getUseQuantity() != null && s.getUseQuantity().compareTo(BigDecimal.ZERO) > 0 &&
//                        s.getTaxUnitPrice() != null && s.getTaxUnitPrice().compareTo(BigDecimal.ZERO) > 0
//                )
//                .collect(Collectors.toList());
//        if (CollectionUtils.isEmpty(confirmDetails)) {
//            return;
//        }
//        //所有物料的分类map
//        Map<Long, String> itemClassify = itemService.listByIds(confirmDetails.stream().map(OutboundConfirmDetail::getItemId)
//                .collect(Collectors.toSet())).stream()
//                .collect(Collectors.toMap(Item::getItemId, a -> StringUtils.isNotBlank(a.getItemClassify())
//                        ? a.getItemClassify() : "其它"));
//        //历史top成本数据
//        Map<String, Cost> topCostMap = super.costService.getTopHistoryDataMap(confirmDetails.stream().map(OutboundConfirmDetail::getProjectNo)
//                .filter(Objects::nonNull).collect(Collectors.toSet()));
//        //历史父类成本数据
//        Map<String, Cost> parentCostMap = super.costService.getHighHistoryDataMap(
//                confirmDetails.stream().map(OutboundConfirmDetail::getProjectNo)
//                        .filter(Objects::nonNull).collect(Collectors.toSet()), CostTypeEnum.OUTBOUND_CONFIRM);
//        //历史子类成本数据
//        //使用拼合参数，可以更精确，只查出需要用到的参数，否则可能存在无用数据
//        //组合查询历史参数
//        Set<String[]> childOld = confirmDetails.stream().filter(a -> StringUtils.isNotEmpty(a.getProjectNo())).map
//                (d -> new String[]{d.getProjectNo(), itemClassify.get(d.getItemId())}).collect(Collectors.toSet());
//        Map<String, Cost> childCostMap = super.costService.getChildHistoryDataMap(childOld);
//        //累加数据
//        confirmDetails.forEach(d -> {
//            BigDecimal totalTaxAmount = d.getUseQuantity().multiply(d.getTaxUnitPrice());
//            String element = d.getProjectNo();
//            Cost top = getTopCost(topCostMap, element);
//            Cost high = getHighCost(parentCostMap, element, CostTypeEnum.OUTBOUND_CONFIRM, top);
//            Cost child = getChildCost(childCostMap, element, itemClassify.get(d.getItemId()), high);
//
//            child.setActualCost(child.getActualCost().add(totalTaxAmount));
//            high.setActualCost(high.getActualCost().add(totalTaxAmount));
//            top.setActualCost(top.getActualCost().add(totalTaxAmount));
//        });
//        //合并所有数据集
//        List<Cost> saveOrUpdateList = new ArrayList<>
//                (topCostMap.size() + parentCostMap.size() + childCostMap.size());
//        saveOrUpdateList.addAll(topCostMap.values());
//        saveOrUpdateList.addAll(parentCostMap.values());
//        saveOrUpdateList.addAll(childCostMap.values());
//        super.saveOrUpdateBatch(saveOrUpdateList);
//    }
//
//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public void doSomething(List<Object> args) {
//        this.operation(args);
//    }
//
//
//}
